"use strict";
module.exports = {
  /**
   * 修改分类
   * @url client/classification/kh/editClassification 前端调用的url参数地址
   * data 请求参数
   * @param {String} params1  参数1
   */
  main: async (event) => {
    let { data = {}, userInfo, util, filterResponse, originalParam } = event;
    let { customUtil, uniID, config, pubFun, vk, db, _, $ } = util;
    let { uid, _id, name, type } = data;
    // 业务逻辑开始-----------------------------------------------------------
    if (!_id || !name || !type) {
      return {
        code: -1,
        msg: "参数错误",
      };
    }
    let resData = await vk.baseDao.selects({
      dbName: "dfb-classification",
      getCount: true,
      getMain: true,
      pageSize: -1,
      whereJson: {
        user_id: uid,
        type: type,
      },
      fieldJson: {
        user_id: false,
      },
    });
    if (resData.find((item) => item.name == name)) {
      return {
        code: -1,
        msg: "分类名称重复",
      };
    }
    const res = await vk.baseDao.updateById({
      dbName: "dfb-classification",
      id: _id,
      dataJson: {
        name,
        type,
      },
      getUpdateData: false, //不会返回修改后的数据对象
    });
    if (!res)
      return {
        code: -1,
        msg: "修改失败",
      };
    // 业务逻辑结束-----------------------------------------------------------
    return {
      code: 0,
      msg: "修改成功",
    };
  },
};
